package com.zhp.tenant;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.UtilityClass;

/**
 * @author zhanghp
 * @since 2024/5/16 10:49
 */
@UtilityClass
public class TenantContextHolder {

    private final ThreadLocal<String> THREAD_TENANT_ID = new TransmittableThreadLocal<>();
    private final ThreadLocal<Boolean> THREAD_SKIP = new TransmittableThreadLocal<>();


    // ===========================租户值设置=============================

    public String getTenantId() {
        return THREAD_TENANT_ID.get();
    }


    public void setTenantId(String tenantId) {
        THREAD_TENANT_ID.set(tenantId);
    }


    // ========================开启租户过滤标识=========================


    public Boolean isSkip() {
        return THREAD_SKIP.get() != null && THREAD_SKIP.get();
    }

    public void skip() {
        THREAD_SKIP.set(Boolean.TRUE);
    }

    public void noSkip() {
        THREAD_SKIP.set(Boolean.FALSE);
    }

    // ==============================移除=================================
    public void clear() {
        THREAD_TENANT_ID.remove();
        THREAD_SKIP.remove();
    }
}
